package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.Result;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.customer.service.ICommonUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 普通用户相关接口
 *
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 用户地址相关接口")
public class AddressBookController {
    @Resource
    private IAddressBookService addressBookService;


    @GetMapping("/defaultAddress")
    @ApiOperation("查询用户默认地址")
    public AddressBookResDTO getDefaultAddress() {
        return addressBookService.getDefaultAddress();
    }


    /**
     * 添加新地址
     *
     * @return
     */

    @PostMapping()
    @ApiOperation("添加新地址")
    public Result addressBookResDTO(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.saveAddressBook(dto);
        return Result.ok();
    }


    @GetMapping("/page")
    @PostMapping()
    @ApiOperation("地址簿列表")
    public PageResult<AddressBookResDTO> getPage(AddressBookPageQueryReqDTO dto) {


        return addressBookService.getPage(dto);
    }


    @GetMapping("/{id}")
    @PostMapping()
    @ApiOperation("地址详情")
    public AddressBookResDTO detailAddress(@PathVariable Long id) {


        return addressBookService.detailAddress(id);
    }


    //PUT  /customer/consumer/address-book/{id}
    @PutMapping("/{id}")
    @ApiOperation("地址编辑")
    public Result editAddress(
            @PathVariable(name = "id") Long id
            , @RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.editAddress(id, dto);
        return Result.ok();
    }

    //PUT  /customer/consumer/address-book/default
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAddress(Integer flag,Long id) {
        addressBookService.setDefaultAddress(flag,id);
        return Result.ok();
    }


    //DELETE   /customer/consumer/address-book/batch
    @DeleteMapping("/batch")
    @ApiOperation("批量删除地址")
    public Result batchDelete(@RequestBody List<Long> ids) {
        addressBookService.removeByIds(ids);
        return Result.ok();
    }

}
